package com.ruoyi.web.controller.stores;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.google.common.net.HttpHeaders;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.system.domain.stores.EquipmentLedger;
import com.ruoyi.system.domain.entity.Upload;
import com.ruoyi.system.service.IEquipmentLedgerService;
import com.ruoyi.system.service.IUploadService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;



import java.io.*;
import java.util.List;

/**
 * @author GE-LV5
 * @create 2023/2/17 13:52
 */
@Api(tags = "设备台账")
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@Slf4j
@RequestMapping("/equipment/ledger")
public class StoresEquipmentLedgerController extends BaseController {

    @Autowired
    private IEquipmentLedgerService equipmentLedgerService;


    @Autowired
    private TokenService tokenService;


    /**
     * 查询设备台账列表
     *
     * @param stores
     * @return
     */
    //@PreAuthorize("@ss.hasPermi('system:ledger:list')")
    @ApiOperation(value = "查询设备台账列表")
    @PostMapping("/list")
    public TableDataInfo list(@RequestBody EquipmentLedger stores) {
        startPage();
        List<EquipmentLedger> list = equipmentLedgerService.selectTransferSlipList(stores);
        return getDataTable(list);
    }


    @ApiOperation("删除设备台账")
    @DeleteMapping("delete/id")
    public AjaxResult deleteList(Long id) {
        equipmentLedgerService.deleteEquipmentLedgerById(id);
        return AjaxResult.success("删除成功！");
    }

    /**
     * 新增设备台账
     */
    //@PreAuthorize("@ss.hasPermi('depot:finished:add')" )
    @Log(title = "设备台账" , businessType = BusinessType.INSERT)
    @ApiOperation(value = "新增-设备台账")
    @PostMapping
    public AjaxResult add(@RequestBody EquipmentLedger depotFinished) {
        return toAjax(equipmentLedgerService.save(depotFinished) ? 1 : 0);
    }

    /**
     * 修改设备台账
     */
    //@PreAuthorize("@ss.hasPermi('depot:finished:edit')" )
    @Log(title = "设备台账" , businessType = BusinessType.UPDATE)
    @ApiOperation(value = "修改-设备台账")
    @PutMapping
    public AjaxResult edit(@RequestBody EquipmentLedger depotStockIn) {
        return toAjax(equipmentLedgerService.updateById(depotStockIn) ? 1 : 0);
    }

    /**
     * 导出设备台账列表
     */
    //@PreAuthorize("@ss.hasPermi('stores:ledger:export')")
    @Log(title = "设备台账", businessType = BusinessType.EXPORT)
    @ApiOperation(value = "导出设备台账列表")
    @PostMapping("/export")
    public AjaxResult export(EquipmentLedger plan) {
        LambdaQueryWrapper<EquipmentLedger> lqw = new LambdaQueryWrapper<EquipmentLedger>(plan);
        List<EquipmentLedger> list = equipmentLedgerService.list(lqw);
        ExcelUtil<EquipmentLedger> util = new ExcelUtil<EquipmentLedger>(EquipmentLedger.class);
        return util.exportExcel(list, "导出设备台账");
    }


    /**
     * 导入操作步骤列表
     */
    @Log(title = "导入设备台账", businessType = BusinessType.IMPORT)
    //@PreAuthorize("@ss.hasPermi('stores:ledger:importDataAdd')")
    @ApiOperation(value = "导入设备台账列表")
    @PostMapping("/importDataAdd")
    public AjaxResult importDataAdd(@ApiParam(value = "导入文件", required = true) MultipartFile file, boolean updateSupport) throws Exception {
        log.info("传进来了");
        ExcelUtil<EquipmentLedger> util = new ExcelUtil<EquipmentLedger>(EquipmentLedger.class);
        List<EquipmentLedger> stepList = util.importExcel(file.getInputStream());
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        String operName = loginUser.getUsername();
        String message = equipmentLedgerService.importArea(stepList,updateSupport, operName);
        return AjaxResult.success(message);
    }



    /**
     * 下载模板
     */
    @ApiOperation(value = "下载模板")
    @GetMapping("/importTemplate")
    public AjaxResult importTemplate()
    {
        List<EquipmentLedger> list = equipmentLedgerService.selectTransferSlipList(new EquipmentLedger());
        ExcelUtil<EquipmentLedger> util = new ExcelUtil<EquipmentLedger>(EquipmentLedger.class);
        return util.importTemplateExcel(list,"下载设备台账数据");
    }




     @Autowired
     private IUploadService iUploadService;

    @RequestMapping("/uploadFile")
    @ApiOperation(value = "文件的上传")
    @ResponseBody
    public AjaxResult uploadFile(@RequestParam("files") MultipartFile[] file, @RequestParam("id")String id){
        String filePath = RuoYiConfig.getUploadPath();
        try {
            for (int i = 0; i < file.length; i++) {
                String filePath1 = FileUploadUtils.upload(filePath, file[i]);
                Upload upload = new Upload();
                upload.setFileName(file[i].getOriginalFilename());
                upload.setUrl(filePath1);
                upload.setMateId(Long.parseLong(id));
                iUploadService.insertUpload(upload);

            }
        } catch (IOException e) {
            e.printStackTrace();
            AjaxResult.error("文件上传失败!");
        }
        return AjaxResult.success("文件上传成功");
    }


    /**
     * 导入模板
     */
    @ApiOperation(value = "导入模板")
    @GetMapping("/import")
    public AjaxResult importEquipment(){
        List<EquipmentLedger> list = equipmentLedgerService.selectEquipmentList(new EquipmentLedger());
        ExcelUtil<EquipmentLedger> util = new ExcelUtil<EquipmentLedger>(EquipmentLedger.class);
        return util.importTemplateExcel(list,"导入设备台账数据");
    }
}
